package com.offcn.service;

import com.offcn.dao.UserDao;
import com.offcn.dao.UserDaoImpl;
import com.offcn.entity.User;
import com.offcn.utils.PageUtils;

import java.util.List;
/**
 * 用户service层接口实现类
 */
public class UserServiceImpl implements UserService {

    private UserDao userDao = new UserDaoImpl();


    @Override
    public User login(String username, String password) {
        return userDao.login(username,password);
    }


    @Override
    public boolean addUser(User user) {
        return userDao.addUser(user)>0;
    }


    @Override
    public PageUtils<User> findByPage(int currentPage, int pageSize, String search) {
        int count = userDao.findByPage("select * from user where name like ?", "%"+search+"%").size();
        List<User> userList = userDao.findByPage("select * from user where name like ? limit ?,?", "%" + search + "%", (currentPage - 1) * pageSize, pageSize);
        PageUtils<User> pageUtils = new PageUtils<>(pageSize,currentPage,count,userList);
        return pageUtils;
    }

    @Override
    public Boolean updateUserById(User user) {
        return userDao.updateUserById(user)>0;
    }

    @Override
    public boolean delAll(String[] uids) {
        try {
            for (String uid : uids) {
                int all = userDao.delAll(uid);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
        return true;
    }


}
